﻿namespace UFIDA.U9.Cust.U9Demo.DeliveryBP
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Text;
    using UFSoft.UBF.AopFrame;
    using UFSoft.UBF.Util.Context;
    using UFSoft.UBF.Util.DataAccess;

    /// <summary>
    /// QueryDoc partial 
    /// </summary>	
    public partial class QueryDoc 
	{	
		internal BaseStrategy Select()
		{
			return new QueryDocImpementStrategy();	
		}		
	}
	
	#region  implement strategy	
	/// <summary>
	/// Impement Implement
	/// 
	/// </summary>	
	internal partial class QueryDocImpementStrategy : BaseStrategy
	{
		public QueryDocImpementStrategy() { }

		public override object Do(object obj)
		{						
			QueryDoc bpObj = (QueryDoc)obj;

            //get business operation context is as follows
            //IContext context = ContextManager.Context	

            //auto generating code end,underside is user custom code
            //and if you Implement replace this Exception Code...
            List<ResultDTO> resultList = new List<ResultDTO>();

            //string sql = "";//SQL语句 
            StringBuilder soSql = new StringBuilder();
            string whereSql = "";

            //if (bpObj.SO > 0)
            //{
            //    whereSql += " and SO=" + bpObj.SO + "";
            //}
            //if (!String.IsNullOrEmpty(bpObj.SO_DocNO))
            //{
            //    whereSql += " and SO.DocNO like '%" + bpObj.SO_DocNO + "%'";
            //}

            //if (bpObj.Customer > 0)
            //{
            //    whereSql += " and SO.OrderBy.Customer.ID=" + bpObj.Customer + "";
            //}

            ////UFIDA.U9.SM.SO.SOLine.EntityList soLineList = UFIDA.U9.SM.SO.SOLine.Finder.FindAll(whereSql);
            //UFIDA.U9.ISV.ItemQty.Proxy.BatchQueryItemQtySVRProxy batchProxy = new ISV.ItemQty.Proxy.BatchQueryItemQtySVRProxy();
            //batchProxy.ItemInfos = new List<ISV.ItemQty.ItemQtyQueryDTOData>();
            //{
            //    ISV.ItemQty.ItemQtyQueryDTOData data = new ISV.ItemQty.ItemQtyQueryDTOData();
            //    data.Item = new ISV.ItemQty.ItemInfo4QtyDTOData();
            //    data.Item.ItemID = new CBO.Pub.Controller.CommonArchiveDataDTOData();
            //    data.Item.ItemID.ID = 1001810130049656;
            //    data.Item.ItemGrade = -1;
            //    data.Item.ItemPotency = -1;
            //    data.Org = new CBO.Pub.Controller.CommonArchiveDataDTOData();
            //    data.Org.ID = 1001805310110255;
            //    batchProxy.ItemInfos.Add(data);
            //}
            //List<ISV.ItemQty.ItemQty4ISVDTOData> itemtList = batchProxy.Do();
            //foreach(ISV.ItemQty.ItemQty4ISVDTOData dto in itemtList)
            //{

            //}
            if (bpObj.PO > 0)
            {
                whereSql += " and po.ID=" + bpObj.PO + "";
            }
            if (!String.IsNullOrEmpty(bpObj.PO_DocNo))
            {
                whereSql += " and po.DocNO like '%" + bpObj.PO_DocNo + "%'";
            }

            if (bpObj.Supplier > 0)
            {
                whereSql += " and supp.ID=" + bpObj.Supplier + "";
            }
            if (!String.IsNullOrEmpty(bpObj.Supplier_Code))
            {
                whereSql += " and (supp.Code like '%" + bpObj.Supplier_Code + "%' or suppTrl.Name like '%" + bpObj.Supplier_Code + "%')";
            }
            if (bpObj.Item > 0)
            {
                whereSql += " and poshipLine.ItemInfo_ItemID=" + bpObj.Item + "";
            }
            if (!String.IsNullOrEmpty(bpObj.Item_Code))
            {
                whereSql += " and (poshipLine.ItemInfo_ItemCode like '%" + bpObj.Item_Code + "%' or poshipLine.ItemInfo_ItemName like '%" + bpObj.Item_Code + "%')";
            }
          
            //if (bpObj.Date != DateTime.MinValue && bpObj.Date != DateTime.MaxValue)
            //{
            //    whereSql += " and po.BusinessDate='" + bpObj.Date + "'";
            //}

            if (whereSql.Length == 0)
            {
                throw new Exception("请至少输入一个查询条件");
            }

            soSql.Append(" SELECT po.DocNo AS DocNo,poLine.DocLineNo AS DocLineNo,poshipLine.SubLineNo,po.BusinessDate");
            soSql.Append(" ,supp.ID as Supplier,supp.Code AS Supplier_Code,suppTrl.Name Supplier_Name");
            soSql.Append(" ,poshipLine.ID as POShipLine,poshipLine.ReqQtyTU AS Qty,poLine.OrderPriceTC AS Price");
            soSql.Append(" ,poshipLine.ItemInfo_ItemID,poshipLine.ItemInfo_ItemCode,poshipLine.ItemInfo_ItemName");
            soSql.Append("  FROM PM_POShipLine poshipLine ");
            soSql.Append("  INNER JOIN PM_POLine poLine on poshipLine.POLine=poLine.ID");
            soSql.Append(" INNER JOIN dbo.PM_PurchaseOrder po ON po.ID=poLine.PurchaseOrder");
            soSql.Append(" INNER JOIN dbo.CBO_Supplier supp ON supp.ID=po.Supplier_Supplier");
            soSql.Append(" INNER JOIN dbo.CBO_Supplier_Trl suppTrl ON suppTrl.ID = supp.ID AND suppTrl.SysMLFlag='zh-CN'");
            soSql.Append(" where 1=1 ");
            soSql.Append(whereSql);
            System.Data.DataSet ds;  //数据集
            DataAccessor.RunSQL(DataAccessor.GetConn(), soSql.ToString(), null, out ds);
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    ResultDTO resultDTO = new ResultDTO();
                    resultDTO.DocNo = dr["DocNo"].ToString();
                    resultDTO.DocLineNo = Convert.ToInt32(dr["DocLineNo"].ToString());
                    resultDTO.DocSubLine = Convert.ToInt32(dr["SubLineNo"].ToString());
                    resultDTO.POShipLine = Convert.ToInt64(dr["POShipLine"].ToString());
                    resultDTO.Supplier = Convert.ToInt64(dr["Supplier"].ToString());
                    resultDTO.Supplier_Code = dr["Supplier_Code"].ToString();
                    resultDTO.Supplier_Name = dr["Supplier_Name"].ToString();
                    resultDTO.Item = Convert.ToInt64(dr["ItemInfo_ItemID"].ToString());
                    resultDTO.Item_Code = dr["ItemInfo_ItemCode"].ToString();
                    resultDTO.Item_Name = dr["ItemInfo_ItemName"].ToString();
                    resultDTO.Qty = Convert.ToDecimal(dr["Qty"].ToString());
                    resultDTO.Price = Convert.ToDecimal(dr["Price"].ToString());

                    resultList.Add(resultDTO);
                }
            }
            else
            {
                throw new Exception("未查询到任何数据！");
            }
            return resultList;
        }		
	}

	#endregion
	
	
}